Electrical Engineering (EE) Exam  >  Electrical Engineering (EE) Questions  >  What is the output of the following code snip... Start Learning for Free
What is the output of the following code snippet?
#include <stdio.h>
int main() {
    int a[] = {1, 2, 3, 4};
    int sum = 0;
    for(int i = 0; i < 4; i++) {
        sum += a[i];
    }
    printf("%d", sum);
    return 0;
}
  • a)
    1
  • b)
    4
  • c)
    20
  • d)
    10
Correct answer is option 'D'. Can you explain this answer?
Verified Answer
What is the output of the following code snippet?#include <stdio.h&...
The given code snippet initializes an integer array a with values {1, 2, 3, 4}. It then declares an integer variable sum and initializes it to 0.
The code then enters a for loop that iterates from i = 0 to i < 4. Within each iteration, the value at index i of the array a is added to the sum variable using the += operator.
After the for loop completes, the printf function is used to print the value of sum, which is 10.
Therefore, the output of the code snippet is 10, making the correct answer D) 10.
View all questions of this test
Most Upvoted Answer
What is the output of the following code snippet?#include <stdio.h&...
The given code snippet initializes an integer array a with values {1, 2, 3, 4}. It then declares an integer variable sum and initializes it to 0.
The code then enters a for loop that iterates from i = 0 to i < 4. Within each iteration, the value at index i of the array a is added to the sum variable using the += operator.
After the for loop completes, the printf function is used to print the value of sum, which is 10.
Therefore, the output of the code snippet is 10, making the correct answer D) 10.
Free Test
Community Answer
What is the output of the following code snippet?#include <stdio.h&...

Explanation:

- Initialization:
- An array `a` is initialized with values {1, 2, 3, 4}.
- Variable `sum` is initialized to 0.

- Loop:
- A `for` loop iterates over the elements of array `a`.
- It adds each element of the array to the `sum` variable.

- Calculation:
- In the first iteration, sum = sum + a[0] = 0 + 1 = 1
- In the second iteration, sum = sum + a[1] = 1 + 2 = 3
- In the third iteration, sum = sum + a[2] = 3 + 3 = 6
- In the fourth iteration, sum = sum + a[3] = 6 + 4 = 10

- Output:
- The final value of `sum` after the loop is 10.
- The code prints the value of `sum` using `printf` function.
- Therefore, the output of the code snippet is 10.

Therefore, the correct answer is option 'D'.
Explore Courses for Electrical Engineering (EE) exam
Question Description
What is the output of the following code snippet?#include <stdio.h>int main() { int a[] = {1, 2, 3, 4}; int sum = 0; for(int i = 0; i < 4; i++) { sum += a[i]; } printf("%d", sum); return 0;}a)1b)4c)20d)10Correct answer is option 'D'. Can you explain this answer? for Electrical Engineering (EE) 2025 is part of Electrical Engineering (EE) preparation. The Question and answers have been prepared according to the Electrical Engineering (EE) exam syllabus. Information about What is the output of the following code snippet?#include <stdio.h>int main() { int a[] = {1, 2, 3, 4}; int sum = 0; for(int i = 0; i < 4; i++) { sum += a[i]; } printf("%d", sum); return 0;}a)1b)4c)20d)10Correct answer is option 'D'. Can you explain this answer? covers all topics & solutions for Electrical Engineering (EE) 2025 Exam. Find important definitions, questions, meanings, examples, exercises and tests below for What is the output of the following code snippet?#include <stdio.h>int main() { int a[] = {1, 2, 3, 4}; int sum = 0; for(int i = 0; i < 4; i++) { sum += a[i]; } printf("%d", sum); return 0;}a)1b)4c)20d)10Correct answer is option 'D'. Can you explain this answer?.
Solutions for What is the output of the following code snippet?#include <stdio.h>int main() { int a[] = {1, 2, 3, 4}; int sum = 0; for(int i = 0; i < 4; i++) { sum += a[i]; } printf("%d", sum); return 0;}a)1b)4c)20d)10Correct answer is option 'D'. Can you explain this answer? in English & in Hindi are available as part of our courses for Electrical Engineering (EE). Download more important topics, notes, lectures and mock test series for Electrical Engineering (EE) Exam by signing up for free.
Here you can find the meaning of What is the output of the following code snippet?#include <stdio.h>int main() { int a[] = {1, 2, 3, 4}; int sum = 0; for(int i = 0; i < 4; i++) { sum += a[i]; } printf("%d", sum); return 0;}a)1b)4c)20d)10Correct answer is option 'D'. Can you explain this answer? defined & explained in the simplest way possible. Besides giving the explanation of What is the output of the following code snippet?#include <stdio.h>int main() { int a[] = {1, 2, 3, 4}; int sum = 0; for(int i = 0; i < 4; i++) { sum += a[i]; } printf("%d", sum); return 0;}a)1b)4c)20d)10Correct answer is option 'D'. Can you explain this answer?, a detailed solution for What is the output of the following code snippet?#include <stdio.h>int main() { int a[] = {1, 2, 3, 4}; int sum = 0; for(int i = 0; i < 4; i++) { sum += a[i]; } printf("%d", sum); return 0;}a)1b)4c)20d)10Correct answer is option 'D'. Can you explain this answer? has been provided alongside types of What is the output of the following code snippet?#include <stdio.h>int main() { int a[] = {1, 2, 3, 4}; int sum = 0; for(int i = 0; i < 4; i++) { sum += a[i]; } printf("%d", sum); return 0;}a)1b)4c)20d)10Correct answer is option 'D'. Can you explain this answer? theory, EduRev gives you an ample number of questions to practice What is the output of the following code snippet?#include <stdio.h>int main() { int a[] = {1, 2, 3, 4}; int sum = 0; for(int i = 0; i < 4; i++) { sum += a[i]; } printf("%d", sum); return 0;}a)1b)4c)20d)10Correct answer is option 'D'. Can you explain this answer? tests, examples and also practice Electrical Engineering (EE) tests.
Explore Courses for Electrical Engineering (EE) exam
Signup to solve all Doubts
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev